﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>

<?php //procesinam post
	if (isset($_POST['submit'])) {
		//$message = print_r($_POST);
		if (($_POST['miestas']) != NULL){
			$miestas = ($_POST['miestas']);
			$rajonas = ($_POST['rajonas']);
			$valstybeid = ($_POST['valstybe']);
			
			//dametam rajona
			$sql = "INSERT INTO `rajonai` (`rajonai`) ";
			$sql .= "VALUES (";
			$sql .= "'". $rajonas . "' ";
			$sql .= ");";
			$result = mysql_query($sql, $connection);
				if ($result) {
					// Success!
					$rajonas_id = mysql_insert_id();
					//$message .= "</br>Client added Successsullyy with id:".$rajonas_id." </br>";
					$sql = "INSERT INTO `miestai` (`miestai`, `rajonaiid`, `valstybesid`) ";
					$sql .= "VALUES (";
					$sql .= "'". $miestas . "', ";
					$sql .= $rajonas_id . ", ";
					$sql .= $valstybeid;
					$sql .= " );";
					//echo "SQL naujam: ".$sql; //viskas OK
					
					$result = mysql_query($sql, $connection);
						if ($result) {
							// Success!
							$temp = mysql_insert_id();
							$to = $_SERVER['HTTP_REFERER'];
							redirect_to($to);
							//$message .= "</br>Tolimas miestas ".$miestas." added Successsullyy!! with ID".$temp."</br>";
						} else {
							// Display error message.
							$message .= "<p>Subject creation failed.</p>";
							$message .= "<p>" . mysql_error() . "</p>";
						};
								
								
							} else {
								// Display error message.
								$message .= "<p>Rajonas creation failed.</p>";
								$message .= "<p>" . mysql_error() . "</p>";
							};
			
		};	
			
		
	};



?>

			
					<article>
						<h2>Gyvenamoji vieta</h2>
	<?php echo $message."</br>";?>
	<form action="add_miestas.php" method="post" name="add"><table width="330" border="0" cellspacing="2" cellpadding="3">
  
  <tr>
    <td>Miestas:</td>
    <td><input name="miestas" type="text" /></td>
  </tr>
  <tr>
    <td>Rajonas:</td>
    <td><input name="rajonas" type="text" /></td>
  </tr>
  <tr>
    <td>Valstybė:</td>
    <td><select name="valstybe">
			<?php 		$query = "SELECT * 
						FROM `valstybes` 
						ORDER BY valstybesid ASC";
						$valstybes_set = mysql_query($query, $connection);
						confirm_query($valstybes_set);
						while ($valstybes = mysql_fetch_array($valstybes_set)) {
							echo "<option value=\"". $valstybes["valstybesid"] . "\"";
							echo ">" . $valstybes["valstybes"] . "</option>";
						};

			?> </select></td>
  </tr>
  <tr>
    <td align="right"><input type="submit" name="submit" value="Submit"></td>
    <td><a href="<?php echo $_SERVER['HTTP_REFERER'];?>">Sugryžti</a></td>
  </tr>
</table></form>


				  </article>
				
            
<?php require("includes/footer.php"); ?>
